package jdt.geom;

/**
 * An instance of this class represents a directed edge
 * 
 * @author Shay
 *
 */
public class DirectedEdge extends Edge {

	/* --- Data Members --- */
	
	/** The start point of the edge	 */
	private Point from;
	
	/** The end point of the edge	 */
	private Point to;
	
	/* --- Ctors. --- */
	
	/**
	 * Construct an edge using the starting point and the ending point
	 * @param from The starting point
	 * @param to The ending point
	 * @throws GeometryException if from == to
	 */
	public DirectedEdge(Point from, Point to) throws GeometryException {
		super(from, to);
		this.from = from;
		this.to = to;
	}
	
	/* --- Public Methods --- */
	
	/**
	 * Undirect this directed edge
	 * @return an Edge
	 */
	public Edge unDirect(){
		Edge undirected = null;
		try {
			undirected = new Edge(from, to);
		} catch (GeometryException e) {
			// can't happen
		}
		return undirected;
	}
	
	/**
	 * Determines the side of the point p from this edge
	 * 
	 * @param p
	 *            The point to check
	 * @return true iff p is from the right of AB (and not on AB)
	 */
	public boolean isRightOf(Point p) {
		double cp = (to.getX() - from.getX()) * (p.getY() - from.getY())
				- (to.getY() - from.getY()) * (p.getX() - from.getX());
		return (cp < 0) ? true : false;
	}
	
	/* --- Equals and Hashcode --- */

	/**
	 * @see Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((from == null) ? 0 : from.hashCode());
		result = prime * result + ((to == null) ? 0 : to.hashCode());
		return result;
	}

	/**
	 * @see Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		DirectedEdge other = (DirectedEdge) obj;
		if (from == null) {
			if (other.from != null)
				return false;
		} else if (!from.equals(other.from))
			return false;
		if (to == null) {
			if (other.to != null)
				return false;
		} else if (!to.equals(other.to))
			return false;
		return true;
	}

	
}
